package com.futures.investm.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.ImageView;

import com.futures.investm.R;
import com.futures.investm.utils.IntentUtils;
import com.futures.investm.weight.AutofitViewPager;
import com.aries.ui.view.title.TitleBarView;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import tech.com.commoncore.base.BaseTitleFragment;

public class HomeFragment extends BaseTitleFragment {
    private final String[] mTitles = {"24小时", "精选", "科技", "新经济", "政策"};
    @BindView(R.id.img_exchange_notice)
    ImageView imgExchangeNotice;
    @BindView(R.id.img_zjlx)
    ImageView imgZjlx;
    @BindView(R.id.img_exchange)
    ImageView imgExchange;
    @BindView(R.id.img_qihuo_baike)
    ImageView imgQihuoBaike;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter mAdapter;
    @BindView(R.id.tl_4)
    TabLayout tabLayout_4;
    @BindView(R.id.vp)
    AutofitViewPager vp;
//    NoScrollViewPager vp;


    public static HomeFragment newInstance() {
        Bundle bundle = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void setTitleBar(TitleBarView titleBar) {

//        View view = View.inflate(mContext, R.layout.search_layout, null);
//        titleBar.addCenterAction(titleBar.new ViewAction(view));
        titleBar
                .setLeftText("首页   ")
//                .setBgColor(Color.TRANSPARENT)
//                .setRightTextDrawable(R.mipmap.xiaoxi_icon)
                .setDividerVisible(false)
                .setLeftTextDrawable(null)
                .setTitleMainTextColor(getResources().getColor(R.color.colorTitleText))
                .setLeftTextColor(getResources().getColor(R.color.colorTitleText))
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        IntentUtils.goMessageActivity(mContext);
                    }
                });
//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                IntentUtils.goSeachActivity(mContext);
//            }
//        });


    }


    @Override
    public int getContentLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        for (int i = 0; i < mTitles.length; i++) {

//            if (i==0){
                mFragments.add(ContentFragment.newInstance(i));
//            }else if (i== 1){
//                mFragments.add(HandPickFragment.newInstance(i));
//            }else {
//                mFragments.add(ContentFragment.newInstance(i));
//            }

        }

//        for (String title : mTitles) {
//            mFragments.add(ContentFragment.newInstance(title));
//        }
        mAdapter = new MyPagerAdapter(getChildFragmentManager());
        vp.setAdapter(mAdapter);
        tabLayout_4.setupWithViewPager(vp);
        vp.setOffscreenPageLimit(5); //预加载
        vp.setCurrentItem(0);

    }


    @Override
    public boolean isRegisterEvent() {
        return true;
    }

    @Subscribe
    public void updateData(IEvent event) {

    }



    @OnClick({R.id.img_exchange_notice, R.id.img_zjlx, R.id.img_exchange, R.id.img_qihuo_baike})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_exchange_notice:
                IntentUtils.goMsg(mContext);
                break;
            case R.id.img_zjlx:
                IntentUtils.goFundActivity(mContext);
                break;
            case R.id.img_exchange:
                IntentUtils.goExchangeActivity(mContext);
                break;
            case R.id.img_qihuo_baike:
                IntentUtils.goArticleActivity(mContext);
                break;
        }
    }

    class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

}










